#!/bin/bash -x


# You need 'gzip', GNU 'tar', a C compiler, sed, pdftex to run this.
# And the file: current gap4rXXX.tar.gz

# Frank Lübeck, Frank.Luebeck@Math.RWTH-Aachen.De for questions and complaints.

export NAME="4r7"
unset CFLAGS

tar xzvpf gap4r*.tar.gz
mv gap$NAME gap4r5
cd gap4r5


# Some packages have a few unpleasant file permissions
# - we remove write permissions for group and world
# - we give read permissions for everyone
# - we give write permissions for user
# then we are only left with some files with unnecessary executable bit set
chmod -R g-w pkg
chmod -R o-w pkg
chmod -R u+r pkg
chmod -R g+r pkg
chmod -R o+r pkg
chmod -R u+w pkg

# we want fat binaries for GMP
cd extern
mv Makefile.in Makefile.in.orig
sed -e "s/configure/configure --enable-fat/" < Makefile.in.orig > Makefile.in
cd ..

# compile GAP
./configure
make config
make -j4
source sysinfo.gap-default32
# reduce dependencies
cd bin/$GAParch

##  if ld supports -l:lib...a syntax
#gcc -g  -export-dynamic  -o gap ariths.o blister.o bool.o c_meths1.o c_type1.o c_oper1.o c_filt1.o c_random.o calls.o code.o compiler.o compstat.o costab.o cyclotom.o dt.o dteval.o exprs.o finfield.o funcs.o gap.o gasman.o gmpints.o gvars.o integer.o intrprtr.o listfunc.o listoper.o lists.o objcftl.o objects.o objfgelm.o objpcgel.o objscoll.o objccoll.o opers.o permutat.o plist.o precord.o range.o rational.o read.o records.o saveload.o scanner.o sctable.o set.o stats.o streams.o string.o sysfiles.o system.o tietze.o vars.o vecgf2.o vec8bit.o vector.o vecffe.o weakptr.o iostream.o macfloat.o intfuncs.o -l:libreadline.a -l:libncurses.a  -l:libm.a  extern/gmp/lib/libgmp.a  -ldl 
# store away until pargap is built

## otherwise find paths and adjust the following:
gcc -g  -export-dynamic  -o gap ariths.o blister.o bool.o c_meths1.o c_type1.o c_oper1.o c_filt1.o c_random.o calls.o code.o compiler.o compstat.o costab.o cyclotom.o dt.o dteval.o exprs.o finfield.o funcs.o gap.o gasman.o gmpints.o gvars.o integer.o intrprtr.o listfunc.o listoper.o lists.o objcftl.o objects.o objfgelm.o objpcgel.o objscoll.o objccoll.o opers.o pperm.o permutat.o plist.o precord.o range.o rational.o read.o records.o saveload.o scanner.o sctable.o set.o stats.o streams.o string.o sysfiles.o system.o trans.o tietze.o vars.o vecgf2.o vec8bit.o vector.o vecffe.o weakptr.o iostream.o macfloat.o intfuncs.o /usr/lib/libreadline.a /usr/lib/libncurses.a  /usr/lib/libm.a /usr/lib/libutil.a extern/gmp/lib/libgmp.a  -ldl 
strip gap
cp gap gapnompi
cp gap.o gap.o.nompi
cd ../..

# we collect all start scripts in 'bin'
cp bin/$GAParch/gac bin

# unpack tools
tar xzf etc/tools.tar.gz

# now the packages
cd pkg

cd ace
./configure ../..
make CC="gcc -Wall -ansi -O2 -static"
strip bin/*/ace
cd ..

### program is not 64-bit clean but we can compile a 32-bit binary
cd anupq-*
./configure 
# outdated old versions? make linux-iX86-gcc2 COPTS="-m32 -g" LOPTS="-m32 -g -static"
make LOPTS=-static
strip bin/*/pq
cd ..

# You may not want a writable directory here! In this case substitute
# the directories 'datagens' and 'dataword' by links to somewhere else.
cd atlasrep
chmod 1777 datagens dataword
cd ..


####   this is for newer Debian/Ubuntu, on old Dapper it is easier, see below
##  cd Browse
##  ./configure
##  # reduce dependencies
##  # for this we need .a libs compiled with -fPIC, done as follows
##  #   apt-get source ncurses
##  #   cd ncurses-*
##  #   debian/rules build CFLAGS="-fPIC"
##  #   mkdir -p /users/luebeck/piclibs/32/ncurses/
##  #   cp -a obj obj-wide /users/luebeck/piclibs/32/ncurses/
##  mv Makefile Makefile.orig
##  sed -e "s/-lpanel -lncurses/-L \/users\/luebeck\/piclibs\/32\/ncurses\/obj\/lib -l:libpanel.a -l:libncurses.a/" <Makefile.orig >Makefile
##  make
##  cd ..

cd Browse
./configure
# to reduce dependencies:
mv Makefile Makefile.orig
sed -e "s/-lpanel -lncurses/\/usr\/lib\/libpanel.a \/usr\/lib\/libncurses.a/" <Makefile.orig >Makefile
make
strip bin/*/*.so
cd ..

# Installation of Carat produces a lot of data, maybe you want to leave 
# this out until a user complains.
# It is not possible to provide precompiled binaries because these have the
# path to some data files burned in.
# Here we install first Gmp in pkg/Gmplib and include only a link to
# the Carat standalone installation which must be done on the users machine.
cd carat
tar xzpf carat-2.1b1.tgz
rm -f bin
#ln -s carat-2.1b1/bin bin
cd carat-2.1b1/functions
tar xzpf gmp-*.tar.gz
cd ..
make TOPDIR=`pwd` Links
# compile for generic i686
cd functions/Gmp
./configure --build=x86_64-pc-linux-gnu --prefix=`pwd`/../../../../Gmplib
# in x86_64:
###  ./configure  --prefix=`pwd`/../../../../Gmplib
make 
make install
cp longlong.h ../../../../Gmplib/include
cd ../../..
rm -rf carat-2.1b1
ln -s ../../local/pkg/CaratStandalone/carat-2.1b1/bin bin
cd ..

# is substituted by "semigroups" package
#cd citrus*
#./configure 
#make
#cd ..

cd cohomolo
#make clean
rm -rf bin/*
./configure 
cd standalone/progs.d
mv makefile makefile.orig
echo 'CFLAGS='$COPTS' -static' > makefile
grep -v "^\(CFLAGS\)" makefile.orig >> makefile
cd ../..
make 
strip bin/*/*
cd ..

cd cvec-*
./configure
make
strip bin/*/*
cd ..

cd example
./configure ../..
make CC="gcc -O2 -Wall -static"
strip bin/*/*
cd ..


# float-5.0.18 has no configure!!!
# we saved a copy of float-0.5.12 from GAP 4.7.2
rm -rf float-0.5.18/
cp -a ../../float-0.5.12 .

cd float*

make clean

./configure --with-mpfr=extern --with-mpfi=extern --with-mpc=extern --with-cxsc=extern
make
mkdir -p ../../bin/$GAParch/extern_libs
cp bin/$GAParch/extern/lib/* ../../bin/$GAParch/extern_libs
cd ..

cd fplsa
./configure ../..
make CC="gcc -static -O2 "
strip bin/*/*
#cd doc
#pdftex manual
#pdftex manual
#makeindex manual
#pdftex manual
#cd ../..
cd ..


##  cd fr
##  ./configure 
##  # we now compile and install locally the gsl and glscblas libs with -fPIC
##  cd extern
##  source GET_LIBRARIES
##  tar xzf gsl*.tar.gz
##  cd gsl-*
##  export CFLAGS="-g -O2 -fPIC"
##  ./configure --prefix=`pwd`/../../bin/i686-pc-linux-gnu-gcc-default32/extern
##  make
##  make install
##  unset CFLAGS
##  cd ../..
##  
##  # for the fr_dll.so we now use the .a libs just produced
##  mv Makefile Makefile.orig
##  sed -e "s/-L -lgsl -L -lgslcblas/-L bin\/i686-pc-linux-gnu-gcc-default32\/extern\/lib\/libgsl.a -L bin\/i686-pc-linux-gnu-gcc-default32\/extern\/lib\/libgslcblas.a -L -lm/" < Makefile.orig > Makefile
##  make
##  cd ..


cd Gauss
cp Makefile.linux.in Makefile.linux.in.orig
echo "" >> Makefile.linux.in
echo "mytarget:" >> Makefile.linux.in
echo "		../../bin/@GAPARCH@/gac -d -o bin/@GAPARCH@/gauss.so src/gauss.c" >> Makefile.linux.in
./configure
mv Makefile.linux.in.orig Makefile.linux.in
make bindir
make mytarget
strip bin/*/*.so
cd ..

cd grape
./configure 
make LDFLAGS=-static
strip bin/*/*
cd ..

cd guava*
./configure 
mkdir bin
make CFLAGS="-O2 -g -static" LDFLAGS="-static" LINKOPT="-static"
gcc -static -o ./src/ctjhai/minimum-weight ./src/ctjhai/minimum-weight.o ./src/ctjhai/minimum-weight-gf2.o  ./src/ctjhai/minimum-weight-gf3.o ./src/ctjhai/popcount.o -lm
make install
strip bin/*/*
cd ..

cd io-*
./configure
make
strip bin/*/*
cd ..

cd kbmag
./configure 
make COPTS="-O2 -g -static"
strip bin/*/*
cd ..

# first install liblinbox-dev and its dependencies
# ???????  configure fails, how to use the GAP GMP?

cd nq-*
./configure
# use CFLAGS on, e.g., Dapper whose gcc doesn't support all the -W options
# (nq_CFLAGS adds -W flags which are not available with old compiler)
make nq_CFLAGS="" LDFLAGS=-static CFLAGS="-O3"
strip bin/*/*
cd ..

# orb
cd orb-*
./configure 
make
strip bin/*/*.so
cd ..

# nothing to do? configure and Makefile are for authors only?
cd qaos
cd ..

# needs Tcl/Tk (/usr/bin/wish)  and GraphViz  (/usr/bin/dot)
cd sgpviz/src
# no longer there? ./configure
make xsemi
make xaut
cd ../..

cd edim
./configure
make
strip bin/*/*.so
cd ..

#  see the pargap documentation how to use this 
#  (in particular, user needs procgroup file)
cd pargap
./configure --with-mpi=MPINU2
mv Makefile Makefile.orig
sed -e 's/ rm -f bin/ rm -f binxxx/' < Makefile.orig > Makefile
# produce gapmpi.o and start script
make 
cp bin/pargap.sh ../../bin/
cd ..
rm -f ALLPKG
# reduce dependencies of gap executable 
cd  ..
cd bin/i686*
rm -f pargapmpi
## see above for the two versions
#gcc -g  -export-dynamic  -o pargapmpi ariths.o blister.o bool.o c_meths1.o c_type1.o c_oper1.o c_filt1.o c_random.o calls.o code.o compiler.o compstat.o costab.o cyclotom.o dt.o dteval.o exprs.o finfield.o funcs.o gap.o gasman.o gmpints.o gvars.o integer.o intrprtr.o listfunc.o listoper.o lists.o objcftl.o objects.o objfgelm.o objpcgel.o objscoll.o objccoll.o opers.o permutat.o plist.o precord.o range.o rational.o read.o records.o saveload.o scanner.o sctable.o set.o stats.o streams.o string.o sysfiles.o system.o tietze.o vars.o vecgf2.o vec8bit.o vector.o vecffe.o weakptr.o iostream.o macfloat.o intfuncs.o gapmpi.o -lpthread ../../pkg/pargap/bin/i686*/libmpi.a -l:libreadline.a -l:libncurses.a  -l:libm.a  extern/gmp/lib/libgmp.a  -ldl 
gcc -g  -export-dynamic  -o pargapmpi ariths.o blister.o bool.o c_meths1.o c_type1.o c_oper1.o c_filt1.o c_random.o calls.o code.o compiler.o compstat.o costab.o cyclotom.o dt.o dteval.o exprs.o finfield.o funcs.o gap.o gasman.o gmpints.o gvars.o integer.o intrprtr.o listfunc.o listoper.o lists.o objcftl.o objects.o objfgelm.o objpcgel.o objscoll.o objccoll.o opers.o pperm.o permutat.o plist.o precord.o range.o rational.o read.o records.o saveload.o scanner.o sctable.o set.o stats.o streams.o string.o sysfiles.o system.o trans.o tietze.o vars.o vecgf2.o vec8bit.o vector.o vecffe.o weakptr.o iostream.o macfloat.o intfuncs.o gapmpi.o -lpthread ../../pkg/pargap/bin/i686*/libmpi.a /usr/lib/libreadline.a /usr/lib/libncurses.a  /usr/lib/libm.a /usr/lib/libutil.a extern/gmp/lib/libgmp.a  -ldl 
mv gapnompi gap
mv gap.o.nompi gap.o

cd ../../pkg

# needs the following Debian packages: 
# libxaw7-dev libxpm-dev libxmu-dev libxt-dev libxext-dev libxpm-dev libx11-dev 
cd xgap
./configure
make 
strip bin/*/xgap
cp bin/xgap.sh ../../bin/
cd ..


# cleanup-don't need to distribute .o files from compiler
cd ..
###  maybe better to distribute them?
#find . -name \*.o -exec file {} \; | grep ": ELF " | cut -f1 -d: | xargs rm -f 

# copy the gapsync directory
mkdir gapsync
cd gapsync
cp -a ~/gap4/dev/DistributionUpdate/rsyncserv/gapsync/* .
cp postgapsynchook.g ../lib/
cat read4tail.g >> ../lib/read4.g
touch callpostsync
cd ..

# make various targets in Makefile-default32 work
cd bin
rm -f gap-default32.sh
ln -s ../local/bin/gap gap-default32.sh
cd ..
